<template>
  <div class="app-container">
    <div class="filter-container">

      <el-button-group class="filter-item">

        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >
          新增
        </el-button>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-refresh"
          @click="refresh"
        >
          刷新
        </el-button>
      </el-button-group>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      height="100%"
      class="table-container"
      highlight-current-row
    >
      <el-table-column

        label="ID"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>



      <el-table-column
        label="用户名"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.user }}
        </template>
      </el-table-column>

      <el-table-column
        label="身份"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.roletext }}
        </template>
      </el-table-column>

      <el-table-column
        label="状态"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.status==1?"启用":'禁用' }}
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.mark }}
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="230"
        align="center"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              :loading="scope.row.refMalaicon"
              icon="el-icon-refresh"
              size="mini"
              @click="repwd(scope.row.id,scope)"
            >
              重置密码
            </el-button>



          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'modify' ? '修改' : '新增'"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-width="120px"
        label-position="right"
      >
        <el-form-item label="用户名">
          <el-input v-model="temp.user" placeholder="用户名"/>
        </el-form-item>
        <el-form-item label="密码" v-if="temp.id==0">
          <el-input type="password" v-model="temp.pwd" placeholder="密码"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="temp.mark" placeholder="备注"/>
        </el-form-item>
        <el-form-item label="用户角色">
          <el-radio-group v-model="temp.role">
            <el-radio :label="1">超级管理员</el-radio>
            <el-radio :label="2">编辑人员</el-radio>
            <el-radio :label="3">审核人员</el-radio>
            <el-radio :label="4">统计人员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户状态">
          <el-radio-group v-model="temp.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="text-right">
        <el-button type="danger" @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getadminList,create_admin,rePwd } from '@/api/user'
import { deepClone } from '@/utils'

const _temp = {
  id:'0',
  user: '',
  pwd: '',
  role: 2,
  status: 1
}

export default {
  components: {
    Pagination
  },
  data() {
    return {
      total: 0,
      list: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        created_at: undefined,
        status: undefined,
        keyword: undefined
      },
      temp: Object.assign({}, _temp),
      dialogVisible: false,
      dialogType: 'create',
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    search() {
      this.fetchData()
    },

    goUserType(mala_id, scope) {

    },
    refresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        created_at: undefined,
        status: undefined,
        keyword: undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getadminList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = Object.assign({}, _temp)
    },
    repwd(id){
      rePwd({id:id}).then(response => {
        this.$alert('请记住新的密码:'+response.data.randPwd, '重置成功', {
          confirmButtonText: '确定',
        });
      })
    },
    add() {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogType = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    edit(scope) {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogType = 'modify'
      this.temp = deepClone(scope.row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    changeStatus(value, scope) {
      setTimeout(() => {
        this.list[scope.$index].status = value
        this.$message({
          message: '更新成功',
          type: 'success'
        })
      }, 300)
    },
    submit() {
      if (this.loading) {
        return
      }
      this.loading = true
      create_admin(this.temp).then(response => {
        this.dialogVisible = false
        this.loading = false
        this.fetchData()
        if(this.temp.id>0){
          this.$message({
            message: '更新成功',
            type: 'success'
          })
          return false;
        }
        this.$alert('请记住你的密码:'+response.data.randPwd, '创建成功', {
          confirmButtonText: '确定',
        });
      },data=>{
        this.loading = false
      })
    }
  }
}
</script>
